/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>Project: jse2302 - Utils
 * <p>Powered by webrx On 2023-06-16 09:08:26
 * <p>Created by IntelliJ IDEA
 *
 * @author  webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Utils {

    public static String getMessageTime(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        try {
            d = sdf.parse(str);

        } catch (Exception e) {

        }
        return getMessageTime(d);
    }

    public static String getMessageTime(long time) {
        return getMessageTime(new Date(time));
    }

    public static String getMessageTime(Calendar c) {
        return getMessageTime(c.getTime());
    }

    public static String getMessageTime(Date d) {
        long t1 = d.getTime();
        long t2 = System.currentTimeMillis();
        String str = String.format("%tF %<tT", t1);
        long mm = (t2 - t1) / 1000 / 60;
        long hh = mm / 60;
        long dd = hh / 24;
        if (mm < 1) {
            str = "刚刚";
        } else if (hh < 1) {
            str = String.format("%d分钟前", mm);
        } else if (dd < 1) {
            str = String.format("%d小时前", hh);
        } else if (dd <= 7) {
            str = String.format("%d天前", dd);
        }
        return str;
    }
}
